using Microsoft.EntityFrameworkCore;
using NLog.Web;
using Quartz;
using Seer.SeerSocket;

var builder = WebApplication.CreateBuilder(args);

// config nlog
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
builder.Host.UseNLog();

// redis
builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "localhost:6379";
    options.InstanceName = "agv_";
});


builder.Services.AddScoped<IDistributedCacheHelper, DistributedCacheHelper>();
builder.Services.AddDbContext<SqliteDbContext>(opt =>
{
    string connStr = builder.Configuration.GetConnectionString("Default")!;
    opt.UseSqlite(connStr);
});

// agv socket
builder.Services.AddSingleton<SeerSocket>(sp => new SeerSocket("10.13.32.11", true));

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


// 需要开启cros
string[] urls = new[] { "*" };
builder.Services.AddCors(options =>
    options.AddDefaultPolicy(builder => builder.WithOrigins(urls)
        .AllowAnyMethod().AllowAnyHeader().AllowCredentials())
);


// Add the required Quartz.NET services
builder.Services.AddQuartz(q =>
{
    q.AddJobAndTrigger<DetectAgvJob>(builder.Configuration);
});
// Add the Quartz.NET hosted service
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);


var app = builder.Build();


if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}


app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
